package com.anti_poverty_detection_system.springboot.controller;

import com.anti_poverty_detection_system.springboot.model.IdentificationCodes;
import com.anti_poverty_detection_system.springboot.model.Regions;
import com.anti_poverty_detection_system.springboot.service.IIdentificationCodesService;
import com.anti_poverty_detection_system.springboot.service.IRegionsService;
import com.anti_poverty_detection_system.springboot.service.dto.IdentificationRegionRequest;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.SecureRandom;
import java.util.Random;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZITONG BAI
 * @since 2024-06-17
 */
@RestController
@RequestMapping("/identificationCodes")
public class IdentificationCodesController {

    @Autowired
    IIdentificationCodesService identificationCodesService;
    @Autowired
    IRegionsService regionsService;
    @GetMapping("get_identification_codes")
    public ResponseEntity<Page<IdentificationCodes>> getIdentificationCodes(
            @RequestParam int page,
            @RequestParam int size,
            @RequestParam(required = false) String keywords
    ) {

        Page<IdentificationCodes> identificationCodesPage = new Page<>(page, size);
        if (keywords == null) {
            Page<IdentificationCodes> identificationCodes = identificationCodesService.getIdentificationCodes(identificationCodesPage);
            return ResponseEntity.ok().body(identificationCodes);
        } else {
            Page<IdentificationCodes> identificationCodes = identificationCodesService.getSearchIdentificationCodes(identificationCodesPage, keywords);
            return ResponseEntity.ok().body(identificationCodes);
        }
    }

    @PostMapping("update_identification_codes")
    public ResponseEntity<String> updateIdentificationCodes(@RequestBody IdentificationRegionRequest identificationRegionRequest) {
        String mergedString = String.join("", identificationRegionRequest.getSelectedRegion());
        Integer codeId = identificationRegionRequest.getCodeId();
        Regions regions=regionsService.getRegionsByRegionName(mergedString);
        IdentificationCodes identificationCodes = identificationCodesService.getById(codeId);
        identificationCodes.setRegion(regions.getRegionName());
        identificationCodesService.saveOrUpdate(identificationCodes);
        return ResponseEntity.ok().body("更新成功");
    }
    @PostMapping("add_identification_codes")
    public ResponseEntity<String> addIdentificationCodes(){
        String code=generateStrongPassword(16);
        IdentificationCodes identificationCodes=new IdentificationCodes();
        identificationCodes.setCode(code);
        identificationCodesService.save(identificationCodes);
        return ResponseEntity.ok().body("身份识别码生成成功");
    }

    // 随机生成身份识别码
    private String generateStrongPassword(int length) {
        final String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new SecureRandom();
        StringBuilder password = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            password.append(chars.charAt(random.nextInt(chars.length())));
        }
        return password.toString();
    }
}
